package level4.net.third_party_api.uploaders {
	import com.adobe.images.JPGEncoder;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	import level4.net.third_party_api.photos_center.RRPhotoCenterStruct;
	import level4.net.web.rfc1867impl.RFC1867ContentType;
	import level4.net.web.rfc1867impl.RFC1867Formatter;

	/**
	大图：723px × 540px
	 小图：300px × 200px	
	 * */
	public class RRPhotoCenterUploader
	{
		public var onOk:Function;
		public var onProcess:Function;
		
		public function RRPhotoCenterUploader() {
		}
		
		/**
		 * 上传文件
		 * @param userId
		 * @param sessionKey
		 * @param appName
		 * @param title
		 * @param comment
		 * @param indexFileName
		 * @param indexFile
		 * @param thumbFileName
		 * @param thumbFile
		 * @param onOk:function(resp:String):void,完成时调用
		 * @param onProcess:function(percent:number):void,上传进度
		 * **/
		public function UploadFile(struct:RRPhotoCenterStruct,indexFileName:String,indexFile:BitmapData, thumbFileName:String, thumbFile:BitmapData):void {
			//1
			var indexBytes:ByteArray=getBytes(indexFile);
			var thumbBytes:ByteArray=getBytes(thumbFile);
			
			//2
			var format:RFC1867Formatter=new RFC1867Formatter();
			
			var req:URLRequest=new URLRequest(struct.url);
				req.method=URLRequestMethod.POST;
				req.contentType=RFC1867Formatter.MultipartFormData;
			
				req.data=format.addValueBody(
					{
						userId: struct.userId,
						sessionKey:struct.sessionKey,
						appName: struct.appName,
						title: struct.title,
						comment: struct.comment
					})
					.addFileBody(indexFileName, indexBytes,RFC1867ContentType.IMAGE_X_PNG, "indexFile")
					.addFileBody(thumbFileName, thumbBytes,RFC1867ContentType.IMAGE_X_PNG, "thumbFile")
					.addTail();
			
			var loader:URLLoader=new URLLoader(req);
			addListeners(loader);
		}
		/**
		 * 获取 图片字节
		 * */
		private function getBytes(bmd:BitmapData):ByteArray {
			var bytes:ByteArray = new JPGEncoder(80).encode(bmd);
			bmd.dispose();
			return bytes;
		}
		
		private function addListeners(loader:URLLoader):void {
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
			loader.addEventListener(ProgressEvent.PROGRESS, onProgress);
		}
		
		private function removeListeners(loader:URLLoader):void {
			loader.removeEventListener(Event.COMPLETE, onComplete);
			loader.removeEventListener(IOErrorEvent.IO_ERROR, onIoError);
			loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
			loader.removeEventListener(ProgressEvent.PROGRESS, onProgress);
		}
		/////////////////////////
		//事件
		/////////////////////////
		private function onComplete(e:Event):void {
			var l:URLLoader=e.target as URLLoader;
			removeListeners(l);
			if (onOk != null) {
				onOk(l.data);
			}
			onOk = null;
		}
		
		private function onIoError(e:IOErrorEvent):void {
			removeListeners(e.target as URLLoader);
		}
		
		private function onSecurityError(e:SecurityErrorEvent):void {
			removeListeners(e.target as URLLoader);
		}
		
		private function onProgress(e:ProgressEvent):void {
			if(onProcess != null){
				onProcess(e.bytesLoaded/e.bytesTotal);
			}
		}
		
		
		
		
		
		
		
		
		
		
		
		/**
		 *	test unit 
		 * @param do2d
		 * 
		 */
		public function TestUpload(do2d:DisplayObject,url:String):void {
			//1
			var bmd:BitmapData=new BitmapData(do2d.width, do2d.height);
			bmd.draw(do2d, null, null, null, null, true);
			var bytes:ByteArray=new JPGEncoder(80).encode(bmd);
			bmd.dispose();
			//2
			var format:RFC1867Formatter=new RFC1867Formatter();
			
			var req:URLRequest=new URLRequest(url);
			req.method=URLRequestMethod.POST;
			req.contentType=RFC1867Formatter.MultipartFormData;
			
			req.data=format.addValueBody(
				{
					userId: "343891780", 
					sessionKey: "2.93dbd0ccbe8eeb4320f6c0319b91f6d7.3600.1306562400-343891780", 
					appName: "carclub", 
					title: 'abc', 
					comment: 'nothing'
				}
			).addFileBody("index.jpg", bytes, "indexFile").addFileBody("b.jpg", bytes, "thumbFile").addTail();
			
			var loader:URLLoader=new URLLoader(req);
			addListeners(loader);
		}
	}
}